﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MLY.ADMIN
{
  public partial class UserCard : Bll.BasePage
  {

    Bll.clsUser user;
    protected void Page_Load(object sender, EventArgs e)
    {

      Response.Expires = 0;
      Response.Cache.SetNoStore();
      Response.AppendHeader("Pragma", "no-cache");

      lblUpdatingUserMsgSucc.Visible = false;
      lblUpdatingUserMsgErr.Visible = false;


      if (!Page.IsPostBack)
      {
        ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];


        int UserId;
        if (g.ParseQueryString(Request.QueryString, "UserId", out UserId))
        {
          Bll.clsUser user = new MLY.Bll.clsUser(UserId);
          this.dateFormatDdl.SelectedValue = user.Data.DefaultDateFormat;
          this.EncodingDdl.SelectedValue = user.Data.EncodingId.HasValue ? user.Data.EncodingId.Value.ToString() : "";
          this.txtUserName.Text = user.Data.UserName;
          this.txtFullName.Text = user.Data.FullName;
          this.txtAddress.Text = user.Data.Address;
          this.txtEmail.Text = user.Data.Email;
          this.txtPassword.Text = user.Data.Password;
          this.txtPhone.Text = user.Data.Phone;
          this.rowsInTableTxt.Text = user.Data.DefaultLinesinGrid.HasValue ? user.Data.DefaultLinesinGrid.ToString() : "";
          DropDownOrganization.DataBind();
          DropDownOrganization.SelectedValue = user.Data.OrganiztionId.ToString();
          DropDownRole.DataBind();
          DropDownRole.SelectedValue = user.Data.RollId.ToString();
          langDdl.SelectedValue = user.Data.Culture;
        }


      }

    }



    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
      int UserId;


      if (g.ParseQueryString(Request.QueryString, "UserId", out UserId))
      {

        user = new MLY.Bll.clsUser(UserId);
      }
      else

        if (Request.QueryString["UserId"] == "new")
        {
          user = new MLY.Bll.clsUser();

        }
      user.Data.DefaultDateFormat = dateFormatDdl.SelectedItem.Text;
      user.Data.EncodingId = this.EncodingDdl.SelectedValue == "" ? null : (int?)Convert.ToInt32(this.EncodingDdl.SelectedValue);
      user.Data.Address = txtAddress.Text;
      user.Data.Email = txtEmail.Text;
      user.Data.FullName = txtFullName.Text;
      user.Data.OrganiztionId = int.Parse(DropDownOrganization.SelectedValue);
      user.Data.Password = txtPassword.Text;
      user.Data.Phone = txtPhone.Text;
      user.Data.RollId = int.Parse(DropDownRole.SelectedValue);
      user.Data.UserName = txtUserName.Text;
      user.Data.Culture = langDdl.SelectedValue;
      int q;
      if (int.TryParse(this.rowsInTableTxt.Text, out q))
        user.Data.DefaultLinesinGrid = q;

      if (Bll.clsUser.IsUsernameFree(txtUserName.Text, UserId))
      {
        user.Save();
        lblUpdatingUserMsgSucc.Visible = true;
        lblUpdatingUserMsgErr.Visible = false;
      }
      else
      {
        lblUpdatingUserMsgSucc.Visible = false;
        lblUpdatingUserMsgErr.Visible = true;
      }
    }


    protected void btnClose_Click(object sender, ImageClickEventArgs e)
    {
      Response.RedirectTo((string)ViewState["Referer"]);
    }




  }
}
